package com.chainshop.restock.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import com.chainshop.common.core.domain.pojo.Restock;
import com.chainshop.common.core.domain.pojo.RestockProduct;
import com.chainshop.common.core.domain.pojo.Stock;
import com.chainshop.common.utils.DateUtils;
import com.chainshop.inboundRecords.domain.InboundRecords;
import com.chainshop.inboundRecords.mapper.InboundRecordsMapper;
import com.chainshop.outboundRecords.domain.OutboundRecords;
import com.chainshop.outboundRecords.mapper.OutboundRecordsMapper;
import com.chainshop.restock.constant.RestockConstants;
import com.chainshop.stock.mapper.StockMapper;
import com.chainshop.utils.RestockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chainshop.restock.mapper.RestockMapper;
import com.chainshop.restock.service.IRestockService;

/**
 * 存储补货请求的Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class RestockServiceImpl implements IRestockService 
{
    @Autowired
    private RestockMapper restockMapper;

    @Autowired
    private InboundRecordsMapper inboundRecordsMapper;

    @Autowired
    private OutboundRecordsMapper outboundRecordsMapper;

    @Autowired
    private StockMapper stockMapper;

    /**
     * 查询存储补货请求的
     * 
     * @param replenishId 存储补货请求的主键
     * @return 存储补货请求的
     */
    @Override
    public Restock selectRestockByReplenishId(Long replenishId)
    {
        return restockMapper.selectRestockByReplenishId(replenishId);
    }

    /**
     * 查询存储补货请求的列表
     * 
     * @param restock 存储补货请求的
     * @return 存储补货请求的
     */
    @Override
    public List<Restock> selectRestockList(Restock restock)
    {
        return restockMapper.selectRestockList(restock);
    }

    /**
     * 新增存储补货请求的
     * 
     * @param restock 存储补货请求的
     * @return 结果
     */
    @Override
    public int insertRestock(Restock restock)
    {
        restock.setReplenishId(RestockUtils.getReplenishId(restock));
        List<RestockProduct> restockProducts = restock.getRestockProducts();
        return restockMapper.insertRestock(restock)+restockMapper.insertRestockProduct(RestockUtils.modifyRestockProducts(restockProducts, restock.getReplenishId()));
    }

    /**
     * 修改存储补货请求的
     * 
     * @param restock 存储补货请求的
     * @return 结果
     */
    @Override
    public int updateRestock(Restock restock)
    {
        restock.setStatus(RestockConstants.REPLENISH_STATUS_WAIT_AUDIT);
        return restockMapper.updateRestock(restock);
    }

    /**
     * 批量删除存储补货请求的
     * 
     * @param replenishIds 需要删除的存储补货请求的主键
     * @return 结果
     */
    @Override
    public int deleteRestockByReplenishIds(Long[] replenishIds)
    {
        return restockMapper.deleteRestockByReplenishIds(replenishIds);
    }

    /**
     * 删除存储补货请求的信息
     * 
     * @param replenishId 存储补货请求的主键
     * @return 结果
     */
    @Override
    public int deleteRestockByReplenishId(Long replenishId)
    {
        return restockMapper.deleteRestockByReplenishId(replenishId);
    }

    @Override
    public Restock selectRestockDetailById(Long storeId) {
        Restock restock = restockMapper.selectRestockDetailById(storeId);
        return restock;
    }

    @Override
    public int updateSaveRestock(Restock restock) {
        restock.setSubmitTime(DateUtils.getNowDate());
        List<RestockProduct> restockProducts = restock.getRestockProducts();
        return restockMapper.updateRestock(restock)+restockMapper.updateRestockProduct(restockProducts);
    }

    @Override
    public int updateSubmitRestock(Restock restock) {
        int row = 0;
        restock.setStatus(RestockConstants.REPLENISH_STATUS_AUDIT);
        if (restockMapper.updateRestock(restock) > 0) {
            List<RestockProduct> restockProducts = restock.getRestockProducts();
            row += restockMapper.updateRestockProduct(restockProducts);

            //更新库存

            restockProducts.forEach( restockProduct -> {
                if (Objects.nonNull(restockProduct.getSupplyingStoreId()) && Objects.nonNull(restockProduct.getProductId())) {
                    //入库记录
                    InboundRecords inboundRecords = new InboundRecords();
                    inboundRecordsMapper.updateInboundRecords(getInboundRecords(inboundRecords,restockProduct,restock));

                    //出库记录
                    OutboundRecords outboundRecords = new OutboundRecords();
                    outboundRecordsMapper.updateOutboundRecords(getOutboundRecords(outboundRecords,restockProduct,restock));

                    //更新库存
                    updateStock(inboundRecords, outboundRecords);
                }

            });

        }

        return row;
    }

    private InboundRecords getInboundRecords(InboundRecords inboundRecords, RestockProduct restockProduct, Restock restock) {
        inboundRecords.setQuantity(restockProduct.getQuantity());
        inboundRecords.setStoreId(restockProduct.getSupplyingStoreId());
        inboundRecords.setProductId(restockProduct.getProductId());
        inboundRecords.setReplenishmentId(restock.getReplenishId());
        inboundRecords.setEntryTime(DateUtils.getNowDate());
        inboundRecords.setEntryType(RestockConstants.REPLENISH_ENTRY_TYPE_RESTOCK);
        return inboundRecords;
    }

    private OutboundRecords getOutboundRecords(OutboundRecords outboundRecords, RestockProduct restockProduct, Restock restock) {
        outboundRecords.setQuantity(restockProduct.getQuantity());
        outboundRecords.setStoreId(restock.getStoreId());
        outboundRecords.setProductId(restockProduct.getProductId());
        outboundRecords.setReplenishmentId(restock.getReplenishId());
        outboundRecords.setOutboundTime(DateUtils.getNowDate());
        outboundRecords.setOutboundType(RestockConstants.REPLENISH_ENTRY_TYPE_DISPATCH);
        return outboundRecords;
    }


    private void updateStock(InboundRecords inboundRecords, OutboundRecords outboundRecords) {
        //入库更新
        Stock inStock = stockMapper.selectStockByInboundRecords(inboundRecords);
        inStock.setStoreId(inboundRecords.getStoreId());
        inStock.setProductId(inboundRecords.getProductId());
        inStock.setStockQty(inStock.getStockQty() + inboundRecords.getQuantity());
        inStock.setUpdateTime(DateUtils.getNowDate());
        stockMapper.updateStock(inStock);

        //出库更新
        Stock outStock = stockMapper.selectStockByOutboundRecords(outboundRecords);
        outStock.setStoreId(outboundRecords.getStoreId());
        outStock.setProductId(outboundRecords.getProductId());
        outStock.setStockQty(outStock.getStockQty() - outboundRecords.getQuantity());
        outStock.setUpdateTime(DateUtils.getNowDate());
        stockMapper.updateStock(outStock);

    }

}
